﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Xml;

using E.Web.UI.Page;
using E.BLL;

using X.Common;

namespace E.Web.Teacher.Mail
{
    public partial class MailConfig : System.Web.UI.Page
    {
        public string act;
        public string maiDay;
        public string mailTime;
        public string mailName;
        public string mailPass;
        public string mailHost;
        public string mailTitle;
        public string mailContent;
        XmlDocument xDoc = new XmlDocument();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                xDoc.Load(Server.MapPath("MailConfig.xml"));
                if(xDoc!=null)
                {
                    XmlNodeList xNodeList = xDoc.SelectNodes(".//root/mail");
                    foreach (XmlElement el in xNodeList)
                    {
                        XmlAttributeCollection xmlAttrList = el.Attributes;
                        if (xmlAttrList[0].Value == "mailday")
                            maiDay = xmlAttrList[1].Value;
                        if (xmlAttrList[0].Value == "mailtime")
                            mailTime = xmlAttrList[1].Value;
                        if (xmlAttrList[0].Value == "mailname")
                            mailName = xmlAttrList[1].Value;
                        if (xmlAttrList[0].Value == "mailpass")
                            mailPass = xmlAttrList[1].Value;
                        if (xmlAttrList[0].Value == "mailhost")
                            mailHost = xmlAttrList[1].Value;
                        if (xmlAttrList[0].Value == "mailtitle")
                            mailTitle = xmlAttrList[1].Value;
                        if (xmlAttrList[0].Value == "mailcontent")
                            mailContent = xmlAttrList[1].Value;
                    }
                }
            }
            act = XRequest.GetString("act");
            if (act == "u")
            {
                if (CheckValue())
                {
                    UpdateMailInfo();
                }
            }
        }
        /// <summary>
        /// 修改WebConfig中的配置值
        /// </summary>
        /// <param name="sKey"></param>
        /// <param name="sValue"></param>
        public  void UpdateMailInfo()
        {
            XmlNodeList xNodeList = xDoc.SelectNodes(".//root/mail");
            foreach (XmlElement el in xNodeList)
            {
                if (el.GetAttribute("name") == "mailday")
                {
                    el.SetAttribute("value", XRequest.GetString("txtMailDay"));
                }
                if (el.GetAttribute("name") == "mailtime")
                {
                    el.SetAttribute("value", XRequest.GetString("StartTimeTxt"));
                }
                if (el.GetAttribute("name") == "mailname")
                {
                    el.SetAttribute("value", XRequest.GetString("txtMailName"));
                }
                if (el.GetAttribute("name") == "mailpass")
                {
                    el.SetAttribute("value", XRequest.GetString("txtMailPass"));
                }
                if (el.GetAttribute("name") == "mailhost")
                {
                    el.SetAttribute("value", XRequest.GetString("txtMailHost"));
                }

                if (el.GetAttribute("name") == "mailtitle")
                {
                    el.SetAttribute("value", XRequest.GetString("txtMailTitle"));
                }
                if (el.GetAttribute("name") == "mailcontent")
                {
                    el.SetAttribute("value", XRequest.GetString("txtMailContent"));
                }
            }
            xDoc.Save(Server.MapPath("MailConfig.xml"));
        }
        /// <summary>
        /// 检查输入正确性
        /// </summary>
        /// <returns></returns>
        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("txtMailDay").Equals(""))
            {
                Alert("间隔时间不能为空");
                rst = false;
            }

            if (XRequest.GetString("StartTimeTxt").Equals(""))
            {
                Alert("开始时间不能为空");
                rst = false;
            }
            if (XRequest.GetString("txtMailName").Equals(""))
            {
                Alert("发件人不能为空");
                rst = false;
            }
            if (XRequest.GetString("txtMailPass").Equals(""))
            {
                Alert("发件人密码不能为空");
                rst = false;
            }
            if (XRequest.GetString("txtMailHost").Equals(""))
            {
                Alert("SMTP主机名不能为空");
                rst = false;
            }
            if (XRequest.GetString("txtMailTitle").Equals(""))
            {
                Alert("邮件标题不能为空");
                rst = false;
            }
            if (XRequest.GetString("txtMailContent").Equals(""))
            {
                Alert("邮件内容不能为空");
                rst = false;
            }
            return rst;
        }
        protected void Alert(string content)
        {
            ExecuteJS("alert('" + content + "');");
        }

        /// <summary>
        /// 执行javascript
        /// </summary>
        /// <param name="content">JS内容</param>
        protected void ExecuteJS(string content)
        {
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">");
            System.Web.HttpContext.Current.Response.Write(content);
            System.Web.HttpContext.Current.Response.Write("</script>");
        }
    }
}